<?php
namespace app\home\controller;
use app\admin\model\UserModel;

use think\Db;
use think\Controller;
# jcl -----用户节点
class Position extends Base
{
	#节点列表
    public function positionList()
    {
    	$param = input('param.');
    	$where = [];
    	$where['pid'] = session('uid');
        
    	$page = $param['page'] ?: 1;	//页数

    	$limit = 10;	//每页条数

    	$offset = ( $page - 1 ) * $limit;	//从那个开始查

    	$type = $param['type'] ?:"";	// 0:全部 1:无效  2:有效
    	if (!empty($type)) {
    		$where['is_effective'] = $type;
    	}
        $where['lay'] = 1; 
    	$childrenId = db('user_position')
    			->where($where)
    			->limit($offset,$limit)
    			->order('lay','ASC')
    			->field('uid,lay')
                ->select();
    	if (empty($childrenId)) {
    		$userInfo = [];
    	}else{
            $childrenAll = [];
            $layAll = [];
            foreach ($childrenId as $k => $v) {
                $childrenAll[] = $v['uid']; 
                $layAll[$v['uid']] = $v['lay']; 
            }
        	$userInfo = db('users')->whereIn('id',$childrenAll)->field('id,phone,pool,type,create_time')->select();
    	}
        // dd($userInfo);
    	if (!empty($userInfo)) {
    		foreach ($userInfo as $k => $v) {
                #直推显示手机号,间推显示加*手机号
                // $userInfo[$k]['phone'] = $layAll[$v['id']] == 1 ? $v['phone']:hideStarFour($v['phone']);
                
                #矿池大于等于10000,算力为矿池币数
                if ($v['pool'] >= 10000) {
                    $userInfo[$k]['suanli'] = $v['pool'];
                }else{
    			    $userInfo[$k]['suanli'] = '0.0000';
                }

    			$v['type'] == 1 ?$typeMsg='無效節點':$typeMsg='有效節點';
    		    $userInfo[$k]['is_effect'] = $typeMsg; 
            }

    	}
    	$count = db('user_position')->where(['pid'=>session('uid'),'lay'=>1])->count();
    	// session('positionCount',$count);
    	return json(['status'=>200,'message'=>'ok','data'=>$userInfo,'count'=>$count?:0]);
    }

    #查询有效无效节点个数
    public function searchEffectNum()
    {
    	$id = session('uid');
    	if (null !== session('positionCount')) {
    		$count = session('positionCount');
    	}else{
    		$count = db('user_position')->where(['pid'=>$id])->count();
    	}
    	#无效节点
    	$noEffect  = db('user_position')->where(['pid'=>$id,'is_effective'=>1])->count();
    	#有效节点
    	$yesEffect = $count - $noEffect; 

    	return json(['status'=>200,'data'=>['noEffect'=>$noEffect,'yesEffect'=>$yesEffect]]);
    }



}
